package com.zsd.android.okhttplib;

public class OkHttpClient {

    private Dispatcher dispatcher;
    private boolean isCanceled;
    private int recount;

    public OkHttpClient() {
        this(new Builder());
    }

    private OkHttpClient(Builder builder) {
        this.dispatcher = builder.dispatcher;
        this.isCanceled = builder.isCanceled;
        this.recount = builder.recount;
    }

    public Call newCall(Request request) {
        return new RealCall(this, request);
    }

    public Dispatcher getDispatcher() {
        return dispatcher;
    }

    public boolean isCanceled() {
        return isCanceled;
    }

    public int getRecount() {
        return recount;
    }

    public static final class Builder {

        private Dispatcher dispatcher;
        private boolean isCanceled;
        //重试次数
        private int recount;

        public Builder() {
            dispatcher = new Dispatcher();
            recount = 3;
        }

        public Builder dispatcher(Dispatcher dispatcher) {
            this.dispatcher = dispatcher;
            return this;
        }

        //表示用户取消请求
        public Builder canceled() {
            isCanceled = true;
            return this;
        }

        public Builder setRecount(int recount) {
            this.recount = recount;
            return this;
        }

        public OkHttpClient build() {
            return new OkHttpClient(this);
        }
    }
}
